package com.cenntro.repair.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.curator.shaded.com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import com.cenntro.common.aliyun.FileUpload;
import com.cenntro.common.base.BaseController;
import com.cenntro.common.model.vo.PageVO;
import com.cenntro.common.util.ConvertorUtils;
import com.cenntro.common.util.result.PageResult;
import com.cenntro.common.util.result.Result;
import com.cenntro.repair.common.RepairEnum;
import com.cenntro.repair.common.SooidTypeEnum;
import com.cenntro.repair.model.dbo.AfterRepairFormDO;
import com.cenntro.repair.model.dbo.AfterRepairRefDO;
import com.cenntro.repair.model.qo.AfterRepairFormQO;
import com.cenntro.repair.model.vo.AfterRepairCompleteVO;
import com.cenntro.repair.model.vo.AfterRepairDetailsVO;
import com.cenntro.repair.model.vo.AfterRepairDynamicVO;
import com.cenntro.repair.model.vo.RepairFormVO;
import com.cenntro.repair.model.vo.RepairListVO;
import com.cenntro.repair.service.AfterRepairFormService;
import com.cenntro.repair.service.AfterRepairLogService;
import com.cenntro.repair.service.AfterRepairRefService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 维修单 
 * </p>
 * @author 
 * @since 2019-07-10
 */
@Slf4j
@Api(value = "维修单 ", tags = { "维修单 " })
@RestController
@RequestMapping("/repair")
public class AfterRepairFormController extends BaseController {

    @Resource
    private AfterRepairFormService afterRepairFormService;

    @Resource
    private AfterRepairLogService afterRepairLogService;

    @Resource
    private AfterRepairRefService afterRepairRefService;

    /**
     * 
    * @Title 
    * @param 
    * @return
    */
    @ApiOperation(value = "维修单 新增", notes = "维修单 新增")
    @RequestMapping("/add")
    public Result add(AfterRepairFormQO repairVO, HttpServletRequest req) {
        log.info(repairVO.toString());
        if (((StandardMultipartHttpServletRequest) req).getFileMap().size() != 0) {
            FileUpload upload = new FileUpload();
            String url = upload.upload(req, "repair");
            repairVO.setUrl(url);
        }
        int repairId = afterRepairFormService.insert(repairVO);
        return Result.ok(ImmutableMap.of("repairId", repairId));
    }

    /**
     * 
    * @Title list
    * @Description 
    *   1、司机帐户  保修中 creatPerson = 自己  状态不等于结束
    *   2、维修人员帐户  
    *       未完成(指派给他，或者他正在维修的) 状态未结束
    *       ，报修中（自己报修 未结束），
    *       报修完成（他报修和他维修 已结束），
    *       其他维修（所有他处理的维修）
    *   
    * @param 
    * @return
     */
    @ApiOperation(value = "维修单 查询", notes = "维修单 查询")
    @RequestMapping("/list")
    public Result list(AfterRepairFormQO repairVO, PageVO pageVO) {
        log.info("{}, {}", repairVO.toString(), pageVO.toString());
        if (repairVO.getRepairStatus().contains(",")) {
            repairVO.setListStatus(Arrays.asList(repairVO.getRepairStatus().split(",")));
        } else {
            repairVO.setListStatus(Arrays.asList(repairVO.getRepairStatus()));
        }
        if (repairVO.getSooid() != null) {
            List<RepairListVO> list = new ArrayList<>();
            // 查询司机的报修列表
            if (SooidTypeEnum.DRIVER.getStatus().equals(repairVO.getAccountType())
                    && "5,6".equals(repairVO.getRepairStatus())) {
                repairVO.setListStatus(Arrays.asList("1"));
                list = afterRepairLogService.selectList(repairVO);
                List<Integer> repairIds = new ArrayList<>();
                for (RepairListVO vo : list) {
                    repairIds.add(vo.getRepairId());
                }
                repairVO.setSooid(null);
                // repairVO.setRepairStatus();
                repairVO.setListStatus(Arrays.asList("5", "6"));
                repairVO.setRepairIds(repairIds);
                list = afterRepairLogService.selectList(repairVO);
                Page<RepairListVO> page = PageHelper.startPage(pageVO.getPage(), pageVO.getLimit(), true);
                return PageResult.ok(list, page.getTotal());
            }
            // // 查询维修人员的维修列表
            // if
            // (SooidTypeEnum.REPAIRMAN.getStatus().equals(repairVO.getAccountType()))
            // {
            // repairVO.setRepairStatus(RepairEnum.REPAIRING.getStatus() + "," +
            // RepairEnum.COMPLETE.getStatus());
            // }
            Page<RepairListVO> page = PageHelper.startPage(pageVO.getPage(), pageVO.getLimit(), true);
            list = afterRepairLogService.selectList(repairVO);
            return PageResult.ok(list, page.getTotal());
        }
        return Result.error("参数错误");
        // if
        // (!SooidTypeEnum.DRIVER.getStatus().equals(repairVO.getAccountType())
        // ||
        // !SooidTypeEnum.REPAIRMAN.getStatus().equals(repairVO.getAccountType())||repairVO.getSooid()==null)
        // {
        // return Result.error("参数错误");
        // }
        //// AfterRepairFormDO afterRepairFormDO =
        // ConvertorUtils.deepConvert(repairVO, AfterRepairFormDO.class);
        // Page<RepairListVO> page = PageHelper.startPage(pageVO.getPage(),
        // pageVO.getLimit(), true);
        // List<RepairListVO>list = new ArrayList<>();
        // // 查询司机的报修列表
        // if
        // (SooidTypeEnum.DRIVER.getStatus().equals(repairVO.getAccountType()))
        // {
        // repairVO.setStatus(RepairEnum.CREATE.getStatus());
        // list = afterRepairLogService.selectList(repairVO);
        // }
        // // 查询维修人员的维修列表
        // if
        // (SooidTypeEnum.REPAIRMAN.getStatus().equals(repairVO.getAccountType()))
        // {
        // }
        //// List<AfterRepairFormDO> AfterRepair =
        // afterRepairFormService.selectPage(afterRepairFormDO);
        // return PageResult.ok(list, page.getTotal());
    }

    /**
     * 
    * @Title update
    * @Description 维修 撤销/开始维修 不同的状态值执行不同的功能
    * @param 
    * @return
     */
    @ApiOperation(value = "维修 撤销/开始维修", notes = "维修 撤销/开始维修")
    @RequestMapping("/updateStatus")
    public Result updateStatus(AfterRepairFormQO repairVO) {
        int i = afterRepairFormService.update(repairVO);
        return i > 0 ? Result.ok() : Result.error("");
    }

    /**
     * 
    * @Title updateComplete
    * @Description 维修完成
    * @param 
    * @return
     */
    @ApiOperation(value = "维修 完成", notes = "维修 完成")
    @RequestMapping("/updateComplete")
    public Result updateComplete(AfterRepairCompleteVO compVO) {
        afterRepairFormService.updateComplete(compVO);
        return Result.ok();
    }

    /**
     * 
    * @Title updateAssign
    * @Description 维修指派
    * @param 
    * @return
     */
    @ApiOperation(value = "维修 指派", notes = "维修 指派")
    @RequestMapping("/updateAssign")
    public Result updateAssign(AfterRepairFormQO qo) {
        AfterRepairRefDO afterRepairRef = afterRepairRefService.selectById(qo.getRefId());
        if (afterRepairRef != null) {
            qo.setSooid(afterRepairRef.getSooid());
            afterRepairFormService.updateAssign(qo);
        }
        return Result.ok();
    }

    /**
     * 
    * @Title history
    * @Description 维修历史
    * @param devId
    * @return  
    * @throws
     */
    @RequestMapping("/historys")
    public Result history(Integer devId) {
        List<RepairFormVO> formList = afterRepairFormService.selectByDevId(devId);
        return PageResult.ok(formList);
    }

    /**
     * 
    * @Title count
    * @Description 维修次数
    * @param devId
    * @return  
    * @throws
     */
    @RequestMapping("/count")
    public Result count(Integer devId) {
        int count = afterRepairFormService.selectCount(ImmutableMap.of("devId", devId));
        return Result.ok(ImmutableMap.of("count", count));
    }

    /**
     * 
    * @Title details
    * @Description 维修单详情
    * @param 
    * @return  
    * @throws
     */
    @ApiOperation(value = "维修 维修单详情", notes = "维修 维修单详情")
    @RequestMapping("/details")
    public Result details(Integer repairId) {
        AfterRepairDetailsVO vo = afterRepairFormService.details(repairId);
        return Result.ok(vo);
    }

    @ApiOperation(value = "维修 维修单动态", notes = "维修 维修单动态")
    @RequestMapping("/dynamic")
    public Result dynamic(Integer repairId) {
        List<AfterRepairDynamicVO> list = afterRepairFormService.dynamic(repairId);
        return Result.ok(list);
    }
}
